﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web.UI.WebControls;

namespace Axe.Web.Controls
{
    public class YearSelector : HardcodedDropDownList
    {
        public YearSelector()
        {
            ToolTip = Resources.Control_Resources.YearSelector_ToolTip;
        }

        [DefaultValue(0)]
        [Bindable(true, BindingDirection.TwoWay)]
        public int LowYearOffset
        {
            get
            {
                object o = ViewState["LowYearOffset"];
                if (o != null)
                    return (int)o;
                return 0;
            }
            set
            {
                ViewState["LowYearOffset"] = value;
            }
        }

        [DefaultValue(0)]
        [Bindable(true, BindingDirection.TwoWay)]
        public int HighYearOffset
        {
            get
            {
                object o = ViewState["HighYearOffset"];
                if (o != null)
                    return (int)o;
                return 0;
            }
            set
            {
                ViewState["HighYearOffset"] = value;
            }
        }

        [Bindable(true, BindingDirection.TwoWay)]
        public int SelectedYear
        {
            get { return Int32.Parse(SelectedValue); }
            set
            {
                int currentYear = DateTime.UtcNow.Year;
                int lowYear = currentYear + LowYearOffset;
                int highYear = currentYear + HighYearOffset;

                int year = value;
                year = Math.Max(year, lowYear);
                year = Math.Min(year, highYear);
                SelectedValue = year.ToString();
            }
        }

        protected HardcodedDropDownListEventArgs FillYears(int low, int high, int selectedYear)
        {
            HardcodedDropDownListEventArgs e = new HardcodedDropDownListEventArgs();

            for (int y = low; y <= high; y++)
            {
                bool select = false;
                if (y == selectedYear)
                    select = true;

                ListItem li = new ListItem(y.ToString(),y.ToString(), select);
                e.BoundItems.Add(li);
            }

            return e;
        }
        
        protected override HardcodedDropDownListEventArgs OnRequestDataItems()
        {
            int currentYear = DateTime.UtcNow.Year;

            if (DesignMode)
            {
                return FillYears(currentYear-5,currentYear+5,currentYear);
            }
            
            if (LowYearOffset > HighYearOffset)
                throw new InvalidOperationException("LowYearOffset cannot be greater than HighYearOffset");
            HardcodedDropDownListEventArgs e = new HardcodedDropDownListEventArgs();


            int lowYear = currentYear + LowYearOffset;
            int highYear = currentYear + HighYearOffset;

            int selectedYear = DateTime.UtcNow.Year; 
            selectedYear = Math.Max(currentYear, lowYear);
            selectedYear = Math.Min(currentYear, highYear);
            return FillYears(lowYear, highYear, selectedYear);
        }
    }
}
